package com.swlz.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swlz.dao.MallProductDAO;
import com.swlz.dto.MallSearch;
import com.swlz.dto.MallProductSearch;
import com.swlz.entity.MallProduct;
import com.swlz.exception.CodeException;
import com.swlz.service.MallProductService;
import com.swlz.vo.PageVO;
import com.swlz.vo.ProductVo;
import com.swlz.vo.UserVO;
import com.swlz.service.impl.MallProductServiceImpl;

/**
 * <pre>
 * 业务逻辑接口具体实现类
 * 商城产品
 * ClassName : com.swlz.service.impl.MallProductServiceImpl
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-28 04:57:55
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class MallProductServiceImpl implements MallProductService {
	private MallProductDAO mallProductDAO = new MallProductDAO();
	private Logger log = LoggerFactory.getLogger(MallProductServiceImpl.class);
	
	@Override
	public int add(MallProduct mallProduct) throws CodeException {
		try {
			return mallProductDAO.insertSelective(mallProduct);
		} catch (SQLException e) {
			throw new CodeException(500, "添加商城产品错误",e);
		}
	}

	@Override
	public int update(MallProduct mallProduct) throws CodeException {
		try {
			return mallProductDAO.updateSelective(mallProduct);
		} catch (SQLException e) {
			throw new CodeException(501, "修改商城产品错误",e);
		}
	}

	@Override
	public int delete(Long productId) throws CodeException {
		try {
			return mallProductDAO.deleteById(productId);
		} catch (SQLException e) {
			throw new CodeException(502, "删除商城产品错误",e);
		}
	}
	@Override
	public MallProduct get(Long productId) throws CodeException {
		try {
			return mallProductDAO.selectById(productId);
		} catch (SQLException e) {
			throw new CodeException(503, "获取商城产品失败",e);
		}
	}

	@Override
	public PageVO<ProductVo> page(MallSearch mallSearch) throws CodeException {
		try {
			PageVO<ProductVo> page = new PageVO<ProductVo>(mallProductDAO.count(mallSearch), mallSearch.getPageNumber(),
					mallSearch.getPageSize());
			return page.setList(mallProductDAO.select(mallSearch, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			e.printStackTrace();// 日志
			throw new RuntimeException("获取用户分页数据失败", e);
		}
	}

	@Override
	public List<MallProduct> list(MallProduct mallProduct, int len) {
		try {
			return mallProductDAO.selectByModel(mallProduct, 0, len);
		} catch (SQLException e) {
			log.warn("获取商城产品列表数据失败",e);
			throw new CodeException(505, "获取商城产品列表数据失败",e);
		}	
	}

	@Override
	public PageVO<MallProduct> page(MallProductSearch mallProduct) throws CodeException {
		try {

			long total = mallProductDAO.countByModel(mallProduct);
			PageVO<MallProduct> page = new PageVO<>(total, mallProduct.getPageNumber(), mallProduct.getPageSize());
			return page.setList(mallProductDAO.selectByModel(mallProduct));
		} catch (SQLException e) {
//			e.printStackTrace();// 日志
//			throw new RuntimeException("获取用户分页数据失败", e);
			log.warn("获取商城产品分页数据失败",e);
			throw new CodeException(504, "获取商城产品分页数据失败",e);
		}
	}
	

	@Override
	public PageVO<ProductVo> pages(MallSearch mallSearch) throws CodeException {
		try {
			PageVO<ProductVo> page = new PageVO<ProductVo>(mallProductDAO.count(mallSearch), mallSearch.getPageNumber(),
					mallSearch.getPageSize());
			return page.setList(mallProductDAO.select(mallSearch, page.getOffset(), page.getRowMax()));
			
		} catch (SQLException e) {
			e.printStackTrace();// 日志
			throw new RuntimeException("获取用户分页数据失败", e);
		
		}
	}

	@Override
	public List<MallProduct> list(MallProduct mallProduct, Integer index, Integer len) throws CodeException {
		List<MallProduct> list = null;
		try {
		 list = mallProductDAO.selectByModel(mallProduct, index, len);
		} catch (SQLException e) {
			log.debug("数据检索失败");
		}
		return list;
	}

@Override
public PageVO<MallProduct> page(MallProduct mallProduct, Integer current, Integer rowMax) throws CodeException {
	return null;
}
}
